package com.itobase.statusDetect.Controller;

import com.itobase.statusDetect.Model.User;
import com.itobase.statusDetect.Repository.UserRepository;
import com.itobase.statusDetect.Service.impl.ExcelService;
import com.itobase.statusDetect.Util.AuthUtil;
import com.itobase.statusDetect.Util.ExcelUtil;
import com.itobase.statusDetect.Util.Message;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author htyu
 * @date 2019/2/23
 */

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    ExcelService excelService;


    @PostMapping(value = "login")
    @ResponseBody
    public Message Login(@RequestBody User user, HttpServletRequest request) {
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.getName(), user.getPassword());
        org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        try {
            subject.login(usernamePasswordToken);   //完成登录
            long sessionInvalidTimeMilli = 10800 * 1000L;//session有效时间
            subject.getSession().setTimeout(sessionInvalidTimeMilli);
            return Message.success("登录成功");
        } catch (Exception e) {
            return Message.error("用户名或密码错误");//返回登录页面
        }
    }


    @RequestMapping(value = "logout",method = RequestMethod.GET)
    @ResponseBody
    public Message logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Message.success("登出成功");
    }

    @PostMapping(value = "register")
    @ResponseBody
    public Message register(@RequestBody User user, HttpServletRequest request) {
        User user_1 = null ;
        user_1 = userRepository.findByName(user.getName());
        if(user_1==null){
            userRepository.save(user);
            return Message.success(user);
        }else{
            return Message.error("用户名已存在");
        }
    }

    @PutMapping("user/changePad")
    public Message changePwd(@RequestParam String old, @RequestParam String newPad){
        try {
            String userID = AuthUtil.getUserId();
            User user = userRepository.findById(userID).get();
            if (user.getPassword().equals(old)) {
                user.setPassword(newPad);
            } else {
                return Message.error("请输入正确的旧密码");
            }
            return Message.success(userRepository.save(user));
        } catch (Throwable throwable) {
            return Message.error(-1,"请先登录");
        }
    }

    @GetMapping("download")
    public Message download(HttpServletResponse response){
        return Message.success(excelService.export2007(response));
    }

    @PostMapping("user/upload")
    public Message upload(@RequestParam MultipartFile file) throws IOException {
        return Message.success(excelService.readUserExcel(file));
    }



}
